package editor.main;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import editor.modelo.Portal;
import editor.modelo.Portal.Clase;
import editor.modelo.EditorPortal;
import editor.modelo.Space;
import java.awt.Component;

public class PortalPanel extends JPanel implements EntityEditor<EditorPortal> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private EditorPortal entidad;
	private JTextField txtSource;
	private JTextField txtTarget;
	private JComboBox comboBoxClase;
	private JComboBox comboBoxTipo;
	private JTextField txtIndexSource;
	private JTextField txtIndexTarg;
	private JTextField txtAncho;
	private JTextField txtLargo;
	private JCheckBox chckbxBidir;
	private JButton btnInvertir;
	private JTextField txtAlto;
	/**
	 * Create the panel.
	 */
	public PortalPanel() {
		setPreferredSize(new Dimension(328, 234));
		setLayout(null);

		comboBoxClase = new JComboBox(EditorPortal.Clase.values());
		//comboBoxClase.setEnabled(false);
		comboBoxClase.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(	(EditorPortal.Clase)comboBoxClase.getSelectedItem() == Portal.Clase.inter_floor){

				}

			}
		});
		comboBoxClase.setBounds(53, 9, 140, 20);
		add(comboBoxClase);

		comboBoxTipo = new JComboBox(EditorPortal.Tipo.values());
		comboBoxTipo.setBounds(53, 31, 140, 20);
		add(comboBoxTipo);

		JLabel lblTipo = new JLabel("Tipo:");
		lblTipo.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblTipo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTipo.setBounds(-3, 34, 46, 14);
		add(lblTipo);

		JLabel lblSource = new JLabel("Desde:");
		lblSource.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblSource.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSource.setBounds(-3, 56, 46, 14);
		add(lblSource);

		txtSource = new JTextField();
		txtSource.setEditable(false);
		txtSource.setColumns(10);
		txtSource.setBounds(53, 56, 129, 20);
		add(txtSource);

		JLabel lblTarg = new JLabel("Hacia:");
		lblTarg.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblTarg.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTarg.setBounds(7, 84, 36, 14);
		add(lblTarg);

		txtTarget = new JTextField();
		txtTarget.setEditable(false);
		txtTarget.setColumns(10);
		txtTarget.setBounds(53, 81, 129, 20);
		add(txtTarget);

		JLabel lblClase = new JLabel("Clase:");
		lblClase.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblClase.setHorizontalAlignment(SwingConstants.RIGHT);
		lblClase.setBounds(-3, 12, 46, 14);
		add(lblClase);

		chckbxBidir = new JCheckBox("Bidireccional");
		chckbxBidir.setBounds(208, 174, 97, 23);
		add(chckbxBidir);

		txtIndexSource = new JTextField();
		txtIndexSource.setEditable(false);
		txtIndexSource.setBounds(188, 56, 117, 20);
		add(txtIndexSource);
		txtIndexSource.setColumns(10);

		txtIndexTarg = new JTextField();
		txtIndexTarg.setEditable(false);
		txtIndexTarg.setColumns(10);
		txtIndexTarg.setBounds(188, 81, 117, 20);
		add(txtIndexTarg);

		txtAncho = new JTextField();
		txtAncho.setBounds(62, 147, 86, 20);
		add(txtAncho);
		txtAncho.setColumns(10);

		txtLargo = new JTextField();
		txtLargo.setBounds(62, 203, 86, 20);
		add(txtLargo);
		txtLargo.setColumns(10);

		JLabel lblAncho = new JLabel("Ancho:");
		lblAncho.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAncho.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblAncho.setBounds(7, 150, 46, 14);
		add(lblAncho);

		JLabel lblLargo = new JLabel("Largo:");
		lblLargo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLargo.setBounds(7, 206, 46, 14);
		add(lblLargo);

		btnInvertir = new JButton("Invertir");
		btnInvertir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Space s = entidad.getSource();
				entidad.setSource(entidad.getTarget());
				entidad.setTarget(s);

				Point2D p = entidad.getPtoSource();
				entidad.setPtoSource(entidad.getPtoTarget());
				entidad.setPtoTarget(p);

				txtSource.setText(entidad.getSource().getId());
				txtTarget.setText(entidad.getTarget().getId());
				txtIndexSource.setText(String.valueOf(entidad.getPtoSource()));
				txtIndexTarg.setText(String.valueOf(entidad.getPtoTarget()));
			}
		});
		btnInvertir.setBounds(216, 106, 89, 23);
		add(btnInvertir);

		JLabel lblAlto = new JLabel("Alto:");
		lblAlto.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAlto.setHorizontalTextPosition(SwingConstants.RIGHT);
		lblAlto.setBounds(7, 178, 46, 14);
		add(lblAlto);

		txtAlto = new JTextField();
		txtAlto.setColumns(10);
		txtAlto.setBounds(62, 175, 86, 20);
		add(txtAlto);

		//comboBoxTipos.setModel(new Model)

	}

	public void setEntity(EditorPortal entity) {
		this.entidad = entity;
		this.comboBoxTipo.setSelectedItem(entity.getTipo());
		this.comboBoxClase.setSelectedItem(entity.getClase());
		this.chckbxBidir.setSelected(entity.isBidirectional());
		this.txtSource.setText(entity.getSource().getId());
		this.txtIndexSource.setText(String.valueOf(entity.getPtoSource().getX())+": "+
				String.valueOf(entity.getPtoSource().getY()));
		this.txtAncho.setText(String.valueOf(entity.getWidth()));
		this.txtAlto.setText(String.valueOf(entity.getDifferential()));
		if(entity.getClase() != Clase.intra_floor){
			this.txtLargo.setEditable(false);
			this.btnInvertir.setEnabled(false);
			//this.chckbxBidir.setEnabled(false);
		}	
		if(entity.getTarget() != null){
			this.txtTarget.setText(entity.getTarget().getId());
			this.txtIndexTarg.setText(String.valueOf(entity.getPtoTarget().getX())+": "+
					String.valueOf(entity.getPtoTarget().getY()));

			this.txtLargo.setText(String.valueOf(entity.getLength()));	
		}
	}

	public EditorPortal getEntity() {

		return entidad;
	}

	public void applyChanges() {
		entidad.setTipo((EditorPortal.Tipo)comboBoxTipo.getSelectedItem());
		entidad.setClase((EditorPortal.Clase)comboBoxClase.getSelectedItem());
		try{
			entidad.setDifferential(Integer.valueOf(txtAlto.getText()));
		}catch(Exception ex){}
		entidad.setBidirectional(chckbxBidir.isSelected());
		if(entidad.getClase() == Clase.intra_floor && entidad.getTarget() != null){
			entidad.fitRelativeToSource(Float.valueOf(txtAncho.getText()),Float.valueOf(txtLargo.getText()));
		}
		//	entidad.setName(this.txtName.getText());
		//	entidad.setTipo((Space.Tipo)this.comboBoxTipos.getSelectedItem());

	}

	public boolean validateEntity() {

		return true;
	}
}
